package com.deyou.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chaychan.library.ExpandableLinearLayout;
import com.deyou.R;
import com.deyou.base.BaseActivity;
import com.deyou.callback.ServerApi;
import com.deyou.model.CollectionBean;
import com.deyou.model.HouseResourceAttrBean;
import com.deyou.model.HouseTypeBean;
import com.deyou.model.LzyResponse;
import com.deyou.util.AESUtil;
import com.deyou.util.AndroidUtils;
import com.deyou.util.Urls;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class HouseTypeListActivity extends BaseActivity {

    @BindView(R.id.tv_mid_title)
    TextView tvMidTitle;
    @BindView(R.id.house_type_list)
    ExpandableLinearLayout houseTypeList;

//    private int dvalue;
    private HouseResourceAttrBean attrBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_type_list);
        ButterKnife.bind(this);
        tvMidTitle.setText("房源类型");
        attrBean = (HouseResourceAttrBean) getIntent().getExtras().getSerializable("houseAttr");
//        dvalue = getIntent().getExtras().getInt("dvalue");
        getHouseTypeList();
    }

    @Override
    protected boolean translucentStatusBar() {
        return super.translucentStatusBar();
    }

    private void initView(List<HouseTypeBean> beanList) {

        houseTypeList.removeAllViews();//清除所有的子View（避免重新刷新数据时重复添加）
        //添加数据
        for (int i = 0; i < beanList.size(); i++) {
            View view = View.inflate(this, R.layout.item_house_type, null);
            HouseTypeBean typeBean = new HouseTypeBean();
            ViewHolder viewHolder = new ViewHolder(view, typeBean);
            viewHolder.refreshUI(beanList.get(i), i);
            houseTypeList.addItem(view);//添加子条目
        }
    }

    private void getHouseTypeList(){
        JSONObject object = new JSONObject();
        try {
            object.put("pid", 14);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String paramJson = object.toString();

        Type type = new TypeToken<LzyResponse<List<HouseTypeBean>>>() {}.getType();
        ServerApi.<LzyResponse<List<HouseTypeBean>>>getData(type, Urls.getHouseTypeList, "", AESUtil.encrypt(paramJson))//
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        showLoading();
                    }
                })//
                .map(new Function<LzyResponse<List<HouseTypeBean>>, List<HouseTypeBean>>() {
                    @Override
                    public List<HouseTypeBean> apply(@NonNull LzyResponse<List<HouseTypeBean>> response) throws Exception {
                        return response.data;
                    }
                })//
                .observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new Observer<List<HouseTypeBean>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull List<HouseTypeBean> beanList) {
                        if(null != beanList && beanList.size() > 0){
                            houseTypeList.setVisibility(View.VISIBLE);
                            initView(beanList);
                        } else {
                            houseTypeList.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();            //请求失败
                        showToast("请求失败");
                        dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });
    }

    @OnClick(R.id.iv_left_back)
    public void onViewClicked() {
        finish();
    }

    class ViewHolder {
        @BindView(R.id.iv_type)
        ImageView ivType;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.whole_house_layout)
        RelativeLayout wholeHouseLayout;

        HouseTypeBean typeBean;

        ViewHolder(View view, HouseTypeBean type) {
            ButterKnife.bind(this, view);
            this.typeBean = type;
        }

        private void refreshUI(final HouseTypeBean bean, int i) {
            if (i == 0){
                ivType.setVisibility(View.VISIBLE);
                ivType.setImageResource(R.drawable.ic_apartment);
            } else if (i == 1){
                ivType.setVisibility(View.VISIBLE);
                ivType.setImageResource(R.drawable.ic_house_room);
            } else if (i == 2){
                ivType.setVisibility(View.VISIBLE);
                ivType.setImageResource(R.drawable.ic_breakfest);
            } else {
                ivType.setVisibility(View.GONE);
            }
            tvType.setText(bean.getDname());

            wholeHouseLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
//                    HouseResourceAttrBean houseTypeAttr = new HouseResourceAttrBean();
                    attrBean.setResourceType(bean.getId());
                    bundle.putSerializable("houseTypeAttr", attrBean);
                    AndroidUtils.startNextActivity(HouseTypeListActivity.this, HouseLocationActivity.class, bundle, false);
                }
            });
        }
    }


}
